package com.example.androidtset;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {

    ViewPager vp;

    private MainActivity mContext;

    int curr = 0;

    int ss = 10001;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == ss) {
                vp.setCurrentItem(curr++ % 3);
                this.sendEmptyMessageDelayed(ss, 3000);
            }
        }
    };

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mContext = (MainActivity) context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.home_fragment, null, false);

//        mContext.setActionBarTitle("首页");

        vp = view.findViewById(R.id.vp);
        ImageView imageView1 = new ImageView(mContext);
        ImageView imageView2 = new ImageView(mContext);
        ImageView imageView3 = new ImageView(mContext);

        ArrayList<ImageView> list = new ArrayList<ImageView>();
        list.add(imageView1);
        list.add(imageView2);
        list.add(imageView3);

        vp.setAdapter(new ImagePageAdapter(list));

        handler.sendEmptyMessageDelayed(ss, 1000);

        return view;
    }

    class ImagePageAdapter extends PagerAdapter {

        List<ImageView> list;

        public ImagePageAdapter(List<ImageView> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
            return view == object;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            list.get(position).setScaleType(ImageView.ScaleType.FIT_XY);

            if (position == 0) {
                list.get(position).setImageResource(R.drawable.banner1);
            } else if (position == 1) {
                list.get(position).setImageResource(R.drawable.banner2);
            } else if (position == 2) {
                list.get(position).setImageResource(R.drawable.banner3);
            }


            container.addView(list.get(position));

            return list.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(list.get(position));
        }
    }
}
